package com.example.ll_video.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ll_video.activity.WebViewActivity;
import com.example.ll_video.adapter.AppInfoAdapter;
import com.example.ll_video.bean.BannerInfo;
import com.example.ll_video.bean.ResultData;
import com.example.ll_video.bean.VideoData;
import com.example.ll_video.constant.Constant;
import com.example.ll_video.utils.HttpUtils;
import com.example.ll_video.utils.ImageLoadUtils;
import com.example.ll_video.utils.JsonUtil;
import com.example.ll_video.R;
import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.HolderCreator;
import com.to.aboomy.banner.IndicatorView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeFragment extends Fragment {

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.scroll_view)
    NestedScrollView scrollView;
    private View view;
    private Context context;
    private AppInfoAdapter appInfoAdapter;
    List<BannerInfo> bannerList = new ArrayList<>();
    List<VideoData> videoDataList = new ArrayList<>();

    private Handler handler = new Handler(Objects.requireNonNull(Looper.myLooper())){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    IndicatorView qyIndicator = new IndicatorView(context)
                            .setIndicatorColor(Color.DKGRAY)
                            .setIndicatorSelectorColor(Color.WHITE);

                    banner.setIndicator(qyIndicator)
                            .setHolderCreator(new ImageHolderCreator())
                            .setPages(bannerList);
                    break;
                case 2:
                    appInfoAdapter = new AppInfoAdapter(context,videoDataList);
                    rvData.setAdapter(appInfoAdapter);

                    appInfoAdapter.setItemClickListener((position, videoData) -> {
                        Intent intent =new Intent(context, WebViewActivity.class);
                        intent.putExtra("url",videoData.getLinkurl());
                        startActivity(intent);
                    });
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home_fragment_math, container, false);
        ButterKnife.bind(this, view);
        context = getActivity();
        getBanner();

        getDataList();

        GridLayoutManager layoutManager = new GridLayoutManager(context,3);
        rvData.setLayoutManager(layoutManager);

        return view;
    }


    @OnClick()
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    public class ImageHolderCreator implements HolderCreator {
        @Override
        public View createView(final Context context, final int index, Object o) {
            BannerInfo banner1 = (BannerInfo) o;
            ImageView iv = new ImageView(context);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ImageLoadUtils.imageLoadUrl(banner1.getPicurl(), iv);
            //内部实现点击事件
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(banner1.getLinkurl())){
                      /*  Intent intent= new Intent();
                        intent.setAction("android.intent.action.VIEW");
                        Uri content_url = Uri.parse(banner1.getLinkurl());
                        intent.setData(content_url);
                        startActivity(intent);*/
                      Intent intent =new Intent(context, WebViewActivity.class);
                      intent.putExtra("url",banner1.getLinkurl());
                      startActivity(intent);
                    }
                }
            });
            return iv;
        }
    }

    private void getBanner(){
        HttpUtils.get(Constant.BANNER, new HttpUtils.HttpCallBack() {
            @Override
            public void onSuccess(ResultData resultData) {
                if (!resultData.getMsg().equals("err") ){
                    bannerList = JsonUtil.jsonToList(resultData.getData(),BannerInfo.class);
                    handler.sendEmptyMessage(1);
                }
            }

            @Override
            public void onFail(String error) {
            }
        });
    }
    private void getDataList(){
        HttpUtils.get(Constant.HOME_DATA_LIST, new HttpUtils.HttpCallBack() {
            @Override
            public void onSuccess(ResultData resultData) {
                if (!resultData.getMsg().equals("err") ){
                    videoDataList = JsonUtil.jsonToList(resultData.getData(),VideoData.class);
                    handler.sendEmptyMessage(2);
                }
            }

            @Override
            public void onFail(String error) {
            }
        });
    }
}
